home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / case.h < prev    next >
C/C++ Source or Header  |  2000-01-06  |  5KB  |  177 lines

  1. // $Id: case.h,v 1.6 2000/01/06 06:46:47 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10.  
  11. #ifndef case_INCLUDED
  12. #define case_INCLUDED
  13.  
  14. #include "config.h"
  15. #ifdef HAVE_WCHAR_H
  16. # include <wchar.h>
  17. #endif
  18.  
  19. //
  20. // NOTE that this class is hard-wired to work on an ASCII machine.
  21. // To make it universal, one should uncomment the constructor and
  22. // make the array "lower" non-static. In such a case, each object
  23. // of type Case that is declared will allocate its own "lower" array.
  24. //
  25. class Case
  26. {
  27.     static char lower[128];
  28.     static char upper[128];
  29.  
  30. public:
  31.  
  32.     static inline bool IsAsciiLower(char c)       { return c == lower[c]; }
  33.     static inline char ToAsciiLower(char c)       { return (c & (char) 0x80) ? c : lower[c]; }
  34.     static inline wchar_t ToAsciiLower(wchar_t c) { return (c < 128 ? (wchar_t) lower[c] : c); }
  35.  
  36.     static inline bool IsAsciiUpper(char c)       { return c == upper[c]; }
  37.     static inline char ToAsciiUpper(char c)       { return (c & (char) 0x80) ? c : upper[c]; }
  38.     static inline wchar_t ToAsciiUpper(wchar_t c) { return (c < 128 ? (wchar_t) upper[c] : c); }
  39.  
  40.     static inline bool IsAsciiAlpha(char c)    { return (c == lower[c] || c == upper[c]); }
  41.     static inline bool IsAsciiAlpha(wchar_t c) { return (c == lower[c] || c == upper[c]); }
  42.  
  43.     //
  44.     // Find the position of the first occurrence of a character within a string.
  45.     // If the character is not foud, return -1.
  46.     //
  47.     static inline int Index(char *s, wchar_t c)
  48.     {
  49.         for (int i = 0; *s != U_NULL; i++, s++)
  50.         {
  51.             if (*s == c)
  52.                 return i;
  53.         }
  54.         return -1;
  55.     }
  56.  
  57.     static inline int Index(wchar_t *s, wchar_t c)
  58.     {
  59.         for (int i = 0; *s != U_NULL; i++, s++)
  60.         {
  61.             if (*s == c)
  62.                 return i;
  63.         }
  64.         return -1;
  65.     }
  66.  
  67.     //
  68.     // Compare two character strings segments of length n in the strings
  69.     // s1 and s2 to check whether or not they are equal. Note that unlike
  70.     // the builtin function "strncmp" the comparison always checks n characters
  71.     // and does not terminate if it encounters a NULL character.
  72.     //
  73.     static inline bool StringSegmentEqual(char *s1, const char *s2, int n)
  74.     {
  75.         for (int i = 0; i < n; i++)
  76.         {
  77.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  78.                 return false;
  79.         }
  80.         return true;
  81.     }
  82.  
  83.     static inline bool StringSegmentEqual(wchar_t *s1, const char *s2, int n)
  84.     {
  85.         for (int i = 0; i < n; i++)
  86.         {
  87.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  88.                 return false;
  89.         }
  90.         return true;
  91.     }
  92.  
  93.     static inline bool StringSegmentEqual(char *s1, const wchar_t *s2, int n)
  94.     {
  95.         for (int i = 0; i < n; i++)
  96.         {
  97.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  98.                 return false;
  99.         }
  100.         return true;
  101.     }
  102.  
  103.     static inline bool StringSegmentEqual(wchar_t *s1, const wchar_t *s2, int n)
  104.     {
  105.         for (int i = 0; i < n; i++)
  106.         {
  107.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  108.                 return false;
  109.         }
  110.         return true;
  111.     }
  112.  
  113.  
  114.     //
  115.     // Compare two null-terminated character strings, s1 and s2
  116.     // to check whether or not they are equal.
  117.     //
  118.     static inline bool StringEqual(char *s1, const char *s2)
  119.     {
  120.         int i;
  121.         for (i = 0; s1[i] && s2[i]; i++)
  122.         {
  123.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  124.                 return false;
  125.         }
  126.         return (s1[i] == s2[i]);
  127.     }
  128.  
  129.     static inline bool StringEqual(wchar_t *s1, const char *s2)
  130.     {
  131.         int i;
  132.         for (i = 0; s1[i] && s2[i]; i++)
  133.         {
  134.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  135.                 return false;
  136.         }
  137.         return (s1[i] == s2[i]);
  138.     }
  139.  
  140.     static inline bool StringEqual(char *s1, const wchar_t *s2)
  141.     {
  142.         int i;
  143.         for (i = 0; s1[i] && s2[i]; i++)
  144.         {
  145.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  146.                 return false;
  147.         }
  148.         return (s1[i] == s2[i]);
  149.     }
  150.  
  151.     static inline bool StringEqual(wchar_t *s1, const wchar_t *s2)
  152.     {
  153.         int i;
  154.         for (i = 0; s1[i] && s2[i]; i++)
  155.         {
  156.             if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i]))
  157.                 return false;
  158.         }
  159.         return (s1[i] == s2[i]);
  160.     }
  161.  
  162.  
  163. //  Lcase()
  164. //  {
  165. //      for (int c = 0; c < 256; c++)
  166. //      {
  167. //          if (isupper(c))
  168. //               lower[c] = tolower(c);
  169. //          else if (islower(c))
  170. //               upper[c] = toupper(c);
  171. //          else lower[c] = upper[c] = c;
  172. //      }
  173. //  }
  174. };
  175.  
  176. #endif /* case_INCLUDED */
  177.